package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/*
类简介：

功能：

时间：2020/4/3

*/
@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void upload(List<String[]> strings) {

        for (String[] string : strings) {

            if (orderSettingDao.isExit(string[0]) > 0){
                orderSettingDao.updateOneDay(string[0],string[1]);
            }else {
                orderSettingDao.insertOneDay(string[0],string[1]);
            }

        }

    }

    @Override
    public ArrayList<Map<String, Object>> findAll(String month) {

        List<OrderSetting> orderSettings = null;

        try {
            orderSettings = orderSettingDao.findAll(month + "-01",month + "-31");
        }catch (Exception e){
            throw new RuntimeException("数据库操作失败");
        }

        ArrayList<Map<String, Object>> result = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            result.add(orderSetting.toMap());
        }

        return result;
    }

    @Override
    public void edit(int number, Date orderDate) {

        System.out.println("number = " + number);
        System.out.println("orderDate = " + orderDate);

        if (orderSettingDao.findByOrderDate(orderDate) > 0){
            orderSettingDao.editOneDay(orderDate,number);
        }else {
            orderSettingDao.insertOneDayByEdit(orderDate,number);
        }


    }
}
